<template>
  <div class="container">
    <!-- 控制框 -->
    <div class="handle-box">
      <el-input v-model="searchWord" placeholder="筛选关键词"></el-input>
    </div>
    <el-table
      height="550px"
      border
      size="small"
      :data="data"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="ID"
        prop="article_id"
        width="50"
        align="center"
      ></el-table-column>
      <el-table-column
        label="标题"
        prop="article_title"
        width="100"
      ></el-table-column>
      <el-table-column label="文章图片" width="110" align="center">
        <template v-slot="scope">
          <img
            :src="attachImageUrl(scope.row.article_picture)"
            style="width: 80px"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="描述"
        prop="article_description"
        span="10"
      ></el-table-column>
      <el-table-column
        span="6"
        label="内容"
        prop="article_summary"
        width="100"
      ></el-table-column>
      <el-table-column
        label="创建者"
        prop="article_user"
        width="100"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        prop="article_data"
        width="100"
      ></el-table-column>
      <!-- 删除功能 -->
      <el-table-column label="操作" width="100" align="center">
        <template v-slot="scope">
          <el-button type="danger" @click="deleteRow(scope.row.article_id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      background
      layout="total, prev, pager, next"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="tableData.length"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>

  <!-- 删除提示框 -->
  <luo-del-dialog
    :delVisible="delVisible"
    @confirm="confirm"
    @cancelRow="delVisible = $event"
  ></luo-del-dialog>
</template>

<script lang="ts">
import { defineComponent, getCurrentInstance, watch, ref, computed } from "vue";
import mixin from "@/mixins/mixin";
import { HttpManager } from "@/api/index";
import LuoDelDialog from "@/components/dialog/LuoDelDialog.vue";
export default defineComponent({
  components: {
    LuoDelDialog,
  },
  setup() {
    const url = `http://localhost:8888/oss/upload`;
    const { proxy } = getCurrentInstance();
    const { routerManager, beforeImgUpload } = mixin();

    const tableData = ref([]); // 记录歌曲，用于显示
    const tempDate = ref([]); // 记录歌曲，用于搜索时能临时记录一份歌曲列表
    const pageSize = ref(5); // 页数
    const currentPage = ref(1); // 当前页

    // 计算当前表格中的数据
    const data = computed(() => {
      return tableData.value.slice(
        (currentPage.value - 1) * pageSize.value,
        currentPage.value * pageSize.value
      );
    });

    const searchWord = ref(""); // 记录输入框输入的内容
    watch(searchWord, () => {
      if (searchWord.value === "") {
        tableData.value = tempDate.value;
      } else {
        tableData.value = [];
        for (let item of tempDate.value) {
          if (
            item.article_title.includes(searchWord.value) ||
            item.article_description.includes(searchWord.value)
          ) {
            tableData.value.push(item);
          }
        }
      }
    });
    getData();

    // 获取歌单信息
    async function getData() {
      tableData.value = [];
      tempDate.value = [];
      const result = (await HttpManager.getArticle()) as ResponseBody;
      tableData.value = result.data;
      tempDate.value = result.data;
      currentPage.value = 1;
      console.log(tableData.value);
    }
    // 获取当前页
    function handleCurrentChange(val) {
      currentPage.value = val;
    }
    function uploadUrl(id) {
      return HttpManager.attachImageUrl(`/songList/img/update?id=${id}`);
    }
    /**
     * 删除
     */
    const idx = ref(-1); // 记录当前要删除的行
    const multipleSelection = ref([]); // 记录当前要删除的列表
    const delVisible = ref(false); // 显示删除框

    async function confirm() {
      const result = (await HttpManager.deleteArticle(
        idx.value
      )) as ResponseBody;
      (proxy as any).$message({
        message: result.message,
        type: result.type,
      });
      if (result.success) getData();
      delVisible.value = false;
    }
    function deleteRow(id) {
      idx.value = id;
      delVisible.value = true;
    }
    function handleSelectionChange(val) {
      multipleSelection.value = val;
    }

    return {
      url,
      searchWord,
      data,
      tableData,
      delVisible,
      pageSize,
      currentPage,
      handleSelectionChange,
      beforeImgUpload,
      attachImageUrl: HttpManager.attachImageUrl,
      uploadUrl,
      handleCurrentChange,
      confirm,
      deleteRow,
    };
  },
});
</script>

<style scoped></style>
